package it.intecs.pisa.smarcos.util;

import it.intecs.pisa.smarcos.base.GestureIntents;


public class MathUtils {
	
	
	public static String calculateSlice(float endX, float startX, float endY, float startY) {
		float x,y;

		x = endX-startX;
		y = -endY+startY;
		
		float m=0;
		
		if(x!=0)
			m=y/x;
		
		float phimez=(float) (Math.PI/2);
		String gesture;
		
		if(m>-phimez && m<phimez)
			{
			if(startX < endX)
				gesture=GestureIntents.INTENT_GESTURE_RIGHT;
			else gesture=GestureIntents.INTENT_GESTURE_LEFT;
			}
		else {
			if(startY < endY)
				gesture=GestureIntents.INTENT_GESTURE_DOWN;
			else gesture=GestureIntents.INTENT_GESTURE_UP;
		}
		
		return gesture;
	}
	
	
	public static float distanceBetweenPoints(float xa,float xb, float ya,float yb)
	{
		float result=0;
		float diffX,diffY;
		
		diffX=xb-xa;
		diffY=yb-ya;
		
		result=(float) Math.sqrt(diffX*diffX+diffY*diffY);
		return result;
	}
	
	
	public static boolean isNumber(String s){
		
		if (s == null || s.length() == 0)
			return false;
			
		int i, j;
		String allowed="1234567890";
		for (i=0; i<s.length(); i++){
			for (j=0; j < allowed.length(); j++){
				if (s.charAt(i) == allowed.charAt(j))
					break;
			}
			if (j == allowed.length())
				return false;
		}
		return true;
	}
	
	public static boolean isInteger(String s){
		
		if (s == null || s.length() == 0)
			return false;
			
		Integer i;	
		try{
			i = new Integer(s);
			
		} catch (NumberFormatException e) {
	
			return false;
		} 
		
		return true;
	}
	
	public static Integer convertToInteger(String s){
		
		if (s == null || s.length() == 0)
			return -1;
			
		Integer i;	
		try{
			i = new Integer(s);
			
		} catch (NumberFormatException e) {
	
			return -1;
		} 
		
		return i;
	}
	
}
